package com.clothing.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.clothing.bean.BusinessJson;
import com.clothing.controller.BaseController;
import com.clothing.entity.Business;
import com.clothing.entity.User;
import com.clothing.entity.User.Status;
import com.clothing.service.BusinessService;
import com.clothing.service.UserService;
import com.clothing.utils.AjaxMsg;
import com.clothing.utils.page.Page;
import com.clothing.utils.page.Pageable;

/**
 * @ClassName: BusinessController 
 * @Description: TODO(商家) 
 * @author Charles 
 * @date 2018年5月27日 上午11:14:04 
 * @version 1.0
 */
@RequestMapping("/admin/business")
@Controller("adminBusinessController")
public class BusinessController extends BaseController
{
    @Autowired
    private UserService userService;

    @Autowired
    private BusinessService businessService;
    
    /**
     * @Title: check 
     * @Description: TODO(审核) 
     * @param @param id
     * @param @param passed
     * @param @return 设定文件 
     * @return String 返回类型  
     * @throws
     */
    @RequestMapping(value = "/check", method = RequestMethod.POST)
    @ResponseBody
    public String check(Long id, Boolean passed)
    {
        if (null == passed)
        {
            return getParameterErrorMsg();
        }

        User user = userService.find(id);
        
        if (null == user)
        {
            return getParameterErrorMsg();
        }

        if (Status.unChecked != user.getStatus())
        {
            return AjaxMsg.failed("已经审核过了");
        }

        if (passed)
        {
            user.setStatus(Status.pass);
        }
        else
        {
            user.setStatus(Status.notPass);
        }
            
        userService.update(user);

        return AjaxMsg.success("审核成功");
    }
    
    /**
     * @Title: list 
     * @Description: TODO(商家列表) 
     * @param @param keyword
     * @param @param status
     * @param @param pageable
     * @param @return 设定文件 
     * @return String 返回类型  
     * @throws
     */
    @RequestMapping(value = "/getList.json", method = RequestMethod.GET)
    @ResponseBody
    public String list(String keyword, Status status, Pageable pageable)
    {
        Page<Business> page = businessService.findPage(keyword, status, pageable);
        JSONArray ja = null;
        
        if (page.getTotal() > 0)
        {
            ja = new JSONArray();
            
            for (Business business : page.getContent())
            {
                User user = business.getUser();
                BusinessJson json = new BusinessJson();
                json.setId(user.getId());
                json.setMobile(user.getMobile());
                json.setNickname(user.getNickname());
                json.setStatus(user.getStatus());
                json.setAddress(business.getAddress());
                json.setIndustry(business.getIndustry().getName());
                json.setName(business.getName());
                json.setPhone(business.getPhone());
                ja.add(json);
            }
        }
        
        return new AjaxMsg(true, page.getTotal(), ja).toString();
    }
}
